<template>
  <div>
    <el-table style="width: 100%" border :data="records">
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="skuName" label="名称" width="width">
      </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width">
      </el-table-column>
      <el-table-column label="默认图片" width="100">
        <template slot-scope="{ row }">
          <img
            v-if="row.skuDefaultImg"
            :src="row.skuDefaultImg"
            style="width: 80px; height: 80px"
          />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量(KG)" width="80">
      </el-table-column>
      <el-table-column prop="price" label="价格(元)" width="80">
      </el-table-column>
      <el-table-column prop="prop" label="操作">
        <template slot-scope="{ row }">
          <el-button
            type="success"
            icon="el-icon-sort-down"
            v-if="row.isSale == 0"
            @click="save(row)"
            size="mini"
          ></el-button>
          <el-button
            type="success"
            v-else
            icon="el-icon-sort-up"
            @click="cancel(row)"
            size="mini"
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="message"
          ></el-button>
          <el-button
            type="info"
            icon="el-icon-info"
            size="mini"
            @click="getSkuInfo(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteSku(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @current-change="getSkuList"
      @size-change="handleSizeChange"
      style="text-align: center; margin-top: 100px"
      background
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
    >
    </el-pagination>

    <el-drawer :visible.sync="show" :show-close="false" size="50%">
      <el-row :gutter="20">
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}</el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag
              type="success"
              v-for="attr in skuInfo.skuAttrValueList"
              :key="attr.id"
              style="margin-right: 10px"
              >{{ attr.valueName }}</el-tag
            >
          </template>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <template>
            <el-carousel height="300px" width="150px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item"
              >
                <img :src="item.imgUrl" style="height: 100%; width: 100%" />
              </el-carousel-item>
            </el-carousel>
          </template>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",

  mounted() {
    this.getSkuList();
  },

  data() {
    return {
      total: 0,
      page: 1,
      limit: 10,
      records: [],
      skuInfo: {},
      show: false,
    };
  },

  methods: {
    async getSkuList(pager = 1) {
      this.page = pager;
      const { limit, page } = this;
      let result = await this.$API.sku.reqSkuList1(page, limit);
      if (result.code == 200) {
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },

    handleSizeChange(limit) {
      this.limit = limit;
      this.getSkuList();
    },

    async save(row) {
      let result = await this.$API.sku.reqSale(row.id);
      if (result.code == 200) {
        this.$message({
          type: "success",
          message: "上架成功",
        });
        this.getSkuList();
      }
    },

    async cancel(row) {
      let result = await this.$API.sku.reqCancel(row.id);
      if (result.code == 200) {
        this.$message({
          type: "success",
          message: "下架成功",
        });
        this.getSkuList();
      }
    },

    message() {
      this.$message({
        type: "info",
        message: "功能正在开发中",
      });
    },

    async getSkuInfo(sku) {
      this.skuInfo = {};
      this.show = true;
      let result = await this.$API.sku.reqSkuById(sku.id);
      if (result.code == 200) {
        this.skuInfo = result.data;
      }
    },

    async deleteSku(sku) {
      let result = await this.$API.sku.reqDeleteById(sku.id);
      if (result.code == 200) {
        this.$message({
          type: "success",
          message: "删除成功",
        });
        this.getSkuList();
      }
    },
  },
};
</script>

<style lang="less" scoped>
.el-row {
  .el-col-5 {
    font-size: 20px;
    text-align: right;
  }
  .el-col {
    margin: 10px 10px;
  }
}
</style> 


<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.el-carousel__button {
  width: 10px;
  height: 10px;
  background-color: blue;
  border-radius: 50%;
}
</style>
    